import 'package:flutter/material.dart';
import 'package:flutter_reader/dao/check_data_manager.dart';
import 'package:flutter_reader/dao/pay_data_manager.dart';
import 'package:flutter_reader/pages/home/check/check_page.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:nice_button/NiceButton.dart';
import 'package:shared_preferences/shared_preferences.dart';

class GiftPage extends StatefulWidget {
  GiftPage({Key key}) : super(key: key);

  @override
  _GiftPageState createState() {
    return _GiftPageState();
  }
}

class _GiftPageState extends State<GiftPage> {

  ///查询到本地阅读时间
  int readTime = 0;

  int coin = 0;
  int checkDay = 0;
  int checkCoin = 0;

  @override
  void initState(){
    checkReadTime();
    loadData();
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  loadData(){
    CheckDao.fetchCheckConfig().then((value){
      setState(() {
        coin = value.data.coin;
        checkDay = value.data.daily.current;
        int coinDay = 1;
        if(checkDay >= 1)
          {
            coinDay = checkDay;
          }
        checkCoin = value.data.daily.lists[coinDay - 1].coin;
      });
    });
    print("送书币---${checkCoin.toString()}");
  }

  checkReadTime() async{
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    setState(() {
      readTime = sharedPreferences.getInt("readTime") ?? 0;
    });
    print("阅读时长：${readTime.toString()}");
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: Text('福利专区',
        style: TextStyle(
          color: Colors.black,
        ),),
        iconTheme: IconThemeData(color: Colors.black),
      ),
      body: Container(
        color: Color.fromRGBO(246, 246, 246, 1),
        child: Column(
          children: <Widget>[
            Expanded(
              child: ListView(
                children: <Widget>[
                  Container(
                    height: ScreenUtil().setHeight(700),
                    color: Colors.white,
                    child: Column(
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.only(top: ScreenUtil().setHeight(50)),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Row(
                                children: <Widget>[
                                  Container(
                                    margin: EdgeInsets.only(left: ScreenUtil().setWidth(50)),
                                    width: ScreenUtil().setWidth(80),
                                    child: Image(
                                      fit: BoxFit.contain,
                                      image: AssetImage("images/fl_coin.png"),
                                    ),
                                  ),
                                  Container(
                                      margin: EdgeInsets.only(left: ScreenUtil().setWidth(50)),
                                      width: ScreenUtil().setWidth(300),
                                      child: FittedBox(
                                        fit: BoxFit.fitWidth,
                                        child: Text("我的书币：${coin.toString()}"),
                                      )
                                  )
                                ],
                              ),
                              Container(
                                margin: EdgeInsets.only(right: ScreenUtil().setWidth(50)),
                                child: Icon(Icons.chevron_right,
                                  color: Colors.black38,),
                              )
                            ],
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.only(top: ScreenUtil().setHeight(50),
                              left: ScreenUtil().setWidth(50),
                              right: ScreenUtil().setWidth(50)),
                          height: ScreenUtil().setHeight(450),
                          decoration: BoxDecoration(
                              gradient: LinearGradient(
                                colors: <Color>[
                                  Color(0xffE0EAFC),
                                  Color(0xffCFDEF3),
                                ],
                              ),
                              image: DecorationImage(
                                colorFilter: new ColorFilter.mode(
                                    Colors.black.withOpacity(0.1), BlendMode.dstATop),
                                image: AssetImage('images/fl_bgImage.png'),
                                fit: BoxFit.cover,
                              ),
                              borderRadius: BorderRadius.all(Radius.circular(10))
                          ),
                          child: Container(
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: <Widget>[
                                Container(
                                  margin: EdgeInsets.only(top: ScreenUtil().setHeight(50)),
                                  padding: EdgeInsets.only(left: ScreenUtil().setWidth(50)),
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: <Widget>[
                                      Container(
                                          width: ScreenUtil().setWidth(500),
                                          child: FittedBox(
                                            child: Text("您已连续签到${checkDay.toString()}天",
                                              style: TextStyle(
                                                  color: Colors.black54,
                                                  fontWeight: FontWeight.w600
                                              ),),
                                          )
                                      ),
                                      Container(
                                        height: ScreenUtil().setHeight(140),
                                        child: NiceButton(
                                          width: ScreenUtil().setWidth(370),
                                          radius: 30,
                                          icon: Icons.monetization_on,
                                          iconColor: Colors.orange,
                                          text: "签到+${checkCoin.toString()}",
                                          fontSize: ScreenUtil().setSp(40),
                                          gradientColors: [Color(0xffef473a), Color(0xffe53935)],
                                          onPressed: () {},
                                        ),
                                      )
                                    ],
                                  ),
                                ),
                                Container(
                                  margin: EdgeInsets.only(left: ScreenUtil().setWidth(50),bottom: ScreenUtil().setHeight(80)),
                                  child: Row(
                                    children: <Widget>[
                                      Container(
                                          width: ScreenUtil().setWidth(500),
                                          child: FittedBox(
                                            fit: BoxFit.fitWidth,
                                            child: Text('已经漏签，补签可赢取书币。'),
                                          )
                                      ),
                                      InkWell(
                                        onTap: (){
                                          Navigator.push(context, MaterialPageRoute(
                                            builder: (context) => CheckPage()
                                          ));
                                        },
                                        child: Container(
                                            width: ScreenUtil().setWidth(200),
                                            child: FittedBox(
                                              fit: BoxFit.fitWidth,
                                              child: Text('立即补签 >',
                                                style: TextStyle(
                                                    color: Color(0xffe53935),
                                                    decoration: TextDecoration.underline
                                                ),),
                                            )
                                        ),
                                      )
                                    ],
                                  ),
                                )
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: ScreenUtil().setHeight(50)),
                    color: Colors.white,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.only(left: ScreenUtil().setWidth(50),
                              top: ScreenUtil().setHeight(80)),
                          child: Text(
                            "阅读福利",
                            style: TextStyle(
                                fontSize: ScreenUtil().setSp(70),
                                fontWeight: FontWeight.w600,
                                fontFamily: "FZXBSJW"
                            ),
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.only(left: ScreenUtil().setWidth(50),
                              right: ScreenUtil().setWidth(50),
                              top: ScreenUtil().setHeight(40)),
                          height: 1,
                          color: Colors.black12,
                        ),
                        Container(
                          margin: EdgeInsets.only(top: ScreenUtil().setHeight(30),
                          left: ScreenUtil().setWidth(50)),
                          child: Text('当前阅读时长：${(readTime / 60.0).toString()} 分钟',
                          style: TextStyle(
                            fontSize: ScreenUtil().setSp(45),
                            color: Colors.grey
                          ),),
                        ),
                        Container(
                          width: ScreenUtil().setWidth(1025),
                          margin: EdgeInsets.only(top: ScreenUtil().setHeight(30),left: ScreenUtil().setWidth(50)),
                          height: ScreenUtil().setWidth(300),
                          child: Row(
                            children: <Widget>[
                              Expanded(
                                flex: 1,
                                child: Center(
                                  child: _coinIcon(50,30),
                                ),
                              ),
                              Expanded(
                                flex: 1,
                                child: Center(
                                  child: _coinIcon(60,60),
                                ),
                              ),
                              Expanded(
                                flex: 1,
                                child: Center(
                                  child: _coinIcon(80,90),
                                ),
                              ),
                              Expanded(
                                flex: 1,
                                child: Center(
                                  child: _coinIcon(100,180),
                                ),
                              ),
                            ],
                          ),
                        ),
                        _LineTypeOne("阅读计时奖励", "阅读时间越久，累计获得书币越多", "最高100书币", "去阅读")
                      ],
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: ScreenUtil().setHeight(50)),
                    color: Colors.white,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.only(left: ScreenUtil().setWidth(50),
                              top: ScreenUtil().setHeight(80)),
                          child: Text(
                            "日常福利",
                            style: TextStyle(
                                fontSize: ScreenUtil().setSp(70),
                              fontWeight: FontWeight.w600,
                              fontFamily: "FZXBSJW"
                            ),
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.only(left: ScreenUtil().setWidth(50),
                              right: ScreenUtil().setWidth(50),
                              top: ScreenUtil().setHeight(40)),
                          height: 1,
                          color: Colors.black12,
                        ),
                        _LineTypeOne("邀请好友", "邀请好友得书币，书币无上限，快来赢取", "100书币/人", "去邀请"),
                        _LineTypeOne("加微信客服", "前往微信，添加客服，领取书币", "+50书币", "去添加"),
                        _LineTypeOne("绑定手机号", "绑定手机号之后获得书币奖励", "+50书币", "去绑定")
                      ],
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: ScreenUtil().setHeight(50)),
                    color: Colors.white,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.only(left: ScreenUtil().setWidth(50),
                              top: ScreenUtil().setHeight(80)),
                          child: Text(
                            "进阶福利",
                            style: TextStyle(
                                fontSize: ScreenUtil().setSp(70),
                                fontWeight: FontWeight.w600,
                              fontFamily: "FZXBSJW"
                            ),
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.only(left: ScreenUtil().setWidth(50),
                              right: ScreenUtil().setWidth(50),
                              top: ScreenUtil().setHeight(40)),
                          height: 1,
                          color: Colors.black12,
                        ),
                        _LineTypeOne("下载游戏", "下载好玩游戏，赢取海量书币", "海量书币", "去下载")
                      ],
                    ),
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  _LineTypeOne(String mainTitle, String subTitle, String gift, String buttonTitle){
    return Container(
      width: ScreenUtil().setWidth(1025),
      margin: EdgeInsets.only(left: ScreenUtil().setWidth(50),top: ScreenUtil().setHeight(50)),
      child: Column(
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Container(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Container(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Container(
                            child: Text(mainTitle,
                              style: TextStyle(
                                  fontSize: ScreenUtil().setSp(60),
                                  fontWeight: FontWeight.w500
                              ),),
                          ),
                          Container(
                            margin: EdgeInsets.only(left: ScreenUtil().setWidth(20)),
                            child: Image(
                              height: ScreenUtil().setHeight(80),
                              image: AssetImage('images/fl_coin.png'),
                            ),
                          ),
                          Container(
                            margin: EdgeInsets.only(left: ScreenUtil().setWidth(20)),
                            child: Text(
                              gift,
                              style: TextStyle(
                                  color: Colors.orange
                              ),
                            ),
                          )
                        ],
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(top: ScreenUtil().setHeight(30)),
                      child: Text(subTitle,
                      style: TextStyle(
                        color: Colors.grey
                      ),),
                    )
                  ],
                ),
              ),
              Container(
                child: RaisedButton(
                  shape: new RoundedRectangleBorder(
                      borderRadius: new BorderRadius.circular(18.0),
                      side: BorderSide(color: Color(0xffe53935))),
                  onPressed: () {},
                  color: Colors.white,
                  textColor: Color(0xffe53935),
                  child: Text(buttonTitle,
                      style: TextStyle(fontSize: ScreenUtil().setSp(40),
                          fontWeight: FontWeight.w600)),
                ),
              ),
            ],
          ),
          Container(
            margin: EdgeInsets.only(top: ScreenUtil().setHeight(50)),
            color: Color.fromRGBO(246, 246, 246, 1),
            height: 1,
          )
        ],
      ),
    );
  }

  _coinIcon(int coin,int time){
    return Container(
      height: ScreenUtil().setHeight(270),
      child: Column(
        children: <Widget>[
          Stack(
            children: <Widget>[
              Image(
                width: ScreenUtil().setWidth(150),
                image: AssetImage(readTime >= time * 60 ? "images/fl_complete.png" : 'images/fl_giftcoin.png'),
              ),
              readTime >= time * 60 ? Container(
                width: ScreenUtil().setWidth(150),
                height: ScreenUtil().setWidth(150),
              ) :
              Container(
                width: ScreenUtil().setWidth(150),
                height: ScreenUtil().setWidth(150),
                child: Center(
                  child: Column(
                    children: <Widget>[
                      Container(
                          margin: EdgeInsets.only(top: ScreenUtil().setWidth(25)),
                          height: ScreenUtil().setWidth(50),
                          child: Center(
                              child: FittedBox(
                                fit: BoxFit.fitHeight,
                                child: Text(coin.toString(),
                                  style: TextStyle(
                                      color: Colors.white
                                  ),),
                              )
                          )
                      ),
                      Container(
                          margin: EdgeInsets.only(bottom: ScreenUtil().setWidth(25)),
                          height: ScreenUtil().setWidth(50),
                          child: Center(
                              child: FittedBox(
                                fit: BoxFit.fitHeight,
                                child: Text("书币",
                                  style: TextStyle(
                                      color: Colors.white
                                  ),),
                              )
                          )
                      ),
                    ],
                  ),
                ),
              )
            ],
          ),
          Container(
            margin: EdgeInsets.only(top: ScreenUtil().setHeight(20)),
            height: ScreenUtil().setHeight(60),
            child: FittedBox(
              fit: BoxFit.fitHeight,
              child: Text(
                  "${time.toString()}分钟",
                style: TextStyle(
                  color: Colors.black54
                ),
              ),
            )
          )
        ],
      ),
    );
  }
}